package com.hmall.trade.listener;


import cn.hutool.core.util.ObjectUtil;
import com.hmall.api.client.PayClient;
import com.hmall.api.domain.dto.PayOrderDTO;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class PayStatusListener {
    private final IOrderService orderService;

     @Autowired
     private PayClient payClient;



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "order.no_pay.delay.queue"),
            exchange = @Exchange(name = "order.no_pay.delay.exchange",type = ExchangeTypes.DIRECT, delayed = "true"),
            key = "no_pay"
    ))
    public void noPayListener(String messsage){
        log.info("messsage:{}", messsage);
        if(ObjectUtil.isNotEmpty(messsage)){
            PayOrderDTO payOrder = payClient.getByBizNo(messsage);
            orderService.handleNoPayOrder(payOrder);
        }
    }




    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "mark.order.pay.queue",durable = "true"),
            exchange = @Exchange(name = "pay.topic",type = ExchangeTypes.TOPIC),
            key = "pay.success"
    ))
    public void listenPaySuccess(Long orderId){
        orderService.markOrderPaySuccess(orderId);
    }
}
